package com.xwaf.platform.web.controller.x;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.utils.http.IpUtil;
import com.xwaf.platform.common.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Api(hidden = true)
@Controller("Client")
@RequestMapping("x/client")
public class ClientController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(ClientController.class);

	@Autowired
	IpUtil ipUtil;

	@ApiOperation(value = "IP 获取")
	@ResponseBody
	@GetMapping(value = "/ip", produces = "application/json;charset=UTF-8")
	public CommonResponse ip() throws Exception {
		logger.info("ip get...");
		// 新浪IP地址查询API接口
		// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=IP地址
		// 淘宝IP地址库API接口
		// http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
		System.out.println(ipUtil.getLocalHostLANAddress());
		return CommonResponse.createCommonResponse();
	}
	
	@ApiOperation(value = "浏览器类型获取")
	@ResponseBody
	@GetMapping(value = "/getUserAgent", produces = "application/json;charset=UTF-8")
	public CommonResponse getUserAgent() throws Exception {
	    //获取浏览器信息
	    String ua = request.getHeader("User-Agent");
	    //转成UserAgent对象
	    UserAgent userAgent = UserAgent.parseUserAgentString(ua);
	    //获取浏览器信息
	    Browser browser = userAgent.getBrowser();
	    //获取系统信息
	    OperatingSystem os = userAgent.getOperatingSystem();
	    //系统名称
	    String system = os.getName();
	    //浏览器名称
	    String browserName = browser.getName();
		return CommonResponse.createCommonResponse();
	}
}
